<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>插件开发 | 微观世界</title>
    <meta name="generator" content="VuePress 1.5.0">
    <link rel="icon" href="/logo.jpg">
    <link rel="manifest" href="/manifest.json">
    <meta name="description" content="计算机软件 + 物联网 + 云计算项目开发、设计、咨询">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
    <meta name="msapplication-TileColor" content="#000000">
    <link rel="preload" href="/assets/css/0.styles.55f00579.css" as="style"><link rel="preload" href="/assets/js/app.92627f89.js" as="script"><link rel="preload" href="/assets/js/2.817fcd47.js" as="script"><link rel="preload" href="/assets/js/21.3ea5972a.js" as="script"><link rel="prefetch" href="/assets/js/10.791ab255.js"><link rel="prefetch" href="/assets/js/11.1dc01403.js"><link rel="prefetch" href="/assets/js/12.c421b677.js"><link rel="prefetch" href="/assets/js/13.8fa15714.js"><link rel="prefetch" href="/assets/js/14.2cca4cf4.js"><link rel="prefetch" href="/assets/js/15.e9f5696c.js"><link rel="prefetch" href="/assets/js/16.62cab094.js"><link rel="prefetch" href="/assets/js/17.6c789b79.js"><link rel="prefetch" href="/assets/js/18.74f9e612.js"><link rel="prefetch" href="/assets/js/19.98ca7c32.js"><link rel="prefetch" href="/assets/js/20.5052a859.js"><link rel="prefetch" href="/assets/js/22.18d546b5.js"><link rel="prefetch" href="/assets/js/23.069cf840.js"><link rel="prefetch" href="/assets/js/3.62c9d964.js"><link rel="prefetch" href="/assets/js/4.58e718d2.js"><link rel="prefetch" href="/assets/js/5.addf51f6.js"><link rel="prefetch" href="/assets/js/6.a0e5b372.js"><link rel="prefetch" href="/assets/js/7.5d4ccb0f.js"><link rel="prefetch" href="/assets/js/8.ad529520.js"><link rel="prefetch" href="/assets/js/9.cc29df21.js">
    <link rel="stylesheet" href="/assets/css/0.styles.55f00579.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">微观世界</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="成功案例" class="dropdown-title"><span class="title">成功案例</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/product/smart-home/" class="nav-link">
  智能家居
</a></li><li class="dropdown-item"><!----> <a href="/product/share-ark/" class="nav-link">
  共享快递柜
</a></li><li class="dropdown-item"><!----> <a href="/product/share-washing/" class="nav-link">
  共享洗衣机
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="开源项目" class="dropdown-title"><span class="title">开源项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/ws-cloud/gateway/" class="nav-link router-link-active">
  ws-cloud-gateway
</a></li></ul></div></div><div class="nav-item"><a href="/contact/" class="nav-link">
  联系我们
</a></div><div class="nav-item"><a href="/about/" class="nav-link">
  关于我们
</a></div><div class="nav-item"><a href="/link/" class="nav-link">
  友情链接
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="成功案例" class="dropdown-title"><span class="title">成功案例</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/product/smart-home/" class="nav-link">
  智能家居
</a></li><li class="dropdown-item"><!----> <a href="/product/share-ark/" class="nav-link">
  共享快递柜
</a></li><li class="dropdown-item"><!----> <a href="/product/share-washing/" class="nav-link">
  共享洗衣机
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="开源项目" class="dropdown-title"><span class="title">开源项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/ws-cloud/gateway/" class="nav-link router-link-active">
  ws-cloud-gateway
</a></li></ul></div></div><div class="nav-item"><a href="/contact/" class="nav-link">
  联系我们
</a></div><div class="nav-item"><a href="/about/" class="nav-link">
  关于我们
</a></div><div class="nav-item"><a href="/link/" class="nav-link">
  友情链接
</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>指南</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/ws-cloud/gateway/1.x/" class="sidebar-link">介绍</a></li><li><a href="/ws-cloud/gateway/1.x/getting-started.html" class="sidebar-link">快速上手</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>插件</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/ws-cloud/gateway/1.x/plugin/" class="sidebar-link">介绍</a></li><li><a href="/ws-cloud/gateway/1.x/plugin/dev.html" class="active sidebar-link">插件开发</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>官方插件</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/ws-cloud/gateway/1.x/plugin/official/discovery.html" class="sidebar-link">discovery plugin</a></li><li><a href="/ws-cloud/gateway/1.x/plugin/official/rewrite.html" class="sidebar-link">rewrite plugin</a></li><li><a href="/ws-cloud/gateway/1.x/plugin/official/tracing.html" class="sidebar-link">tracing plugin</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="插件开发"><a href="#插件开发" class="header-anchor">#</a> 插件开发</h1> <p>要开发一个插件非常简单：</p> <ol><li><p>在 <code>gateway/app/plugins</code> 目录下创建一个lua文件，如：<code>demo_plugin.lua</code></p></li> <li><p>在各钩子函数中编写对应方法，如果在对应阶段不需要处理可以不定义该阶段对应的钩子函数。各阶段钩子函数参考文档：<a href="https://moonbingbing.gitbooks.io/openresty-best-practices/content/ngx_lua/phase.html" target="_blank" rel="noopener noreferrer">https://moonbingbing.gitbooks.io/openresty-best-practices/content/ngx_lua/phase.html<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <div class="language-lua extra-class"><pre class="language-lua"><code><span class="token comment">-- gateway/app/plugins/demo_plugin.lua</span>

 <span class="token keyword">local</span> _M <span class="token operator">=</span> <span class="token punctuation">{</span>
     name <span class="token operator">=</span> <span class="token string">&quot;demo&quot;</span><span class="token punctuation">,</span>
     desc <span class="token operator">=</span> <span class="token string">&quot;测试插件&quot;</span><span class="token punctuation">,</span>
     version <span class="token operator">=</span> <span class="token number">1.0</span>
 <span class="token punctuation">}</span>

 <span class="token comment">-- 对应 init 阶段</span>
 <span class="token keyword">function</span> _M<span class="token punctuation">.</span><span class="token function">do_in_init</span><span class="token punctuation">(</span>route<span class="token punctuation">)</span>
     <span class="token comment">-- body</span>
 <span class="token keyword">end</span>

 <span class="token comment">-- 对应 init_worker 阶段</span>
 <span class="token keyword">function</span> _M<span class="token punctuation">.</span><span class="token function">do_in_init_worker</span><span class="token punctuation">(</span>route<span class="token punctuation">)</span>
     <span class="token comment">-- body</span>
 <span class="token keyword">end</span>

 <span class="token comment">-- 对应 rewrite 阶段</span>
 <span class="token keyword">function</span> _M<span class="token punctuation">.</span><span class="token function">do_in_rewrite</span><span class="token punctuation">(</span>route<span class="token punctuation">)</span>
     <span class="token comment">-- body</span>
 <span class="token keyword">end</span>

 <span class="token comment">-- 对应 access 阶段</span>
 <span class="token keyword">function</span> _M<span class="token punctuation">.</span><span class="token function">do_in_access</span><span class="token punctuation">(</span>route<span class="token punctuation">)</span>
     <span class="token comment">-- body</span>
 <span class="token keyword">end</span>

 <span class="token comment">-- 对应 content 阶段</span>
 <span class="token keyword">function</span> _M<span class="token punctuation">.</span><span class="token function">do_in_content</span><span class="token punctuation">(</span>route<span class="token punctuation">)</span>
     <span class="token comment">-- body</span>
 <span class="token keyword">end</span>

 <span class="token comment">-- 对应 balancer 阶段</span>
 <span class="token keyword">function</span> _M<span class="token punctuation">.</span><span class="token function">do_in_balancer</span><span class="token punctuation">(</span>route<span class="token punctuation">)</span>
     <span class="token comment">-- body</span>
 <span class="token keyword">end</span>

 <span class="token comment">-- 对应 body_filter 阶段</span>
 <span class="token keyword">function</span> _M<span class="token punctuation">.</span><span class="token function">do_in_body_filter</span><span class="token punctuation">(</span>route<span class="token punctuation">)</span>
     <span class="token comment">-- body</span>
 <span class="token keyword">end</span>

 <span class="token comment">-- 对应 log 阶段</span>
 <span class="token keyword">function</span> _M<span class="token punctuation">.</span><span class="token function">do_in_log</span><span class="token punctuation">(</span>route<span class="token punctuation">)</span>
     <span class="token comment">-- body</span>
 <span class="token keyword">end</span>

 <span class="token keyword">return</span> _M
</code></pre></div><p>参数 route 是一个 table</p> <div class="language-lua extra-class"><pre class="language-lua"><code>route <span class="token operator">=</span> <span class="token punctuation">{</span>
    prefix
    status
    service_name
    protocol
    plugins
<span class="token punctuation">}</span>
</code></pre></div><blockquote><p>需要注意的是，init 和 init_worker 阶段不能发起网络请求，需要在 <code>ngx.timer</code> 中执行，这个是 openresty 本身的限制</p></blockquote></li></ol> <h1 id="插件注册"><a href="#插件注册" class="header-anchor">#</a> 插件注册</h1> <p>修改 <code>conf/app.json</code>，在 plugins 字段中加上插件对应的文件名（不用加后缀）</p> <div class="language-json extra-class"><pre class="language-json"><code>
<span class="token punctuation">{</span>
    <span class="token property">&quot;plugins&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token string">&quot;default_plugin&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;discovery_plugin&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;tracing_plugin&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;rewrite_plugin&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;demo_plugin&quot;</span>  <span class="token comment">// 注册 demo_plugin 插件</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre></div><h1 id="插件配置"><a href="#插件配置" class="header-anchor">#</a> 插件配置</h1> <p>目前插件配置还不支持在后台面板中设置（开发中），需要手动将配置写到 etcd 中，参考 <code>t/app/init/etcd_dada.t</code> 的 <code>=== TEST 1: init route</code>，在 <code>pipelined_requests eval</code> 中增加加 request 就可以了（注意同时需要增加 <code>response_body eval</code>）</p> <div class="language- extra-class"><pre class="language-text"><code>{
    &quot;prefix&quot;: &quot;/openapi/demo1&quot;,
    &quot;status&quot;: 1,
    &quot;service_name&quot;: &quot;demo1&quot;,
    &quot;protocol&quot;: &quot;http&quot;,
    &quot;plugins&quot;: [
        &quot;discovery&quot;,
        &quot;tracing&quot;,
        &quot;rewrite&quot;
    ],
    &quot;props&quot;: {
        &quot;rewrite_url_regex&quot; : &quot;^/openapi/(.*)/&quot;,
        &quot;rewrite_replace&quot; : &quot;/openapi/&quot;
    }
}
</code></pre></div><p>参数解析：</p> <ul><li>prefix: 匹配路由前缀</li> <li>status: 1 - 有效；0 - 无效</li> <li>service_name: 服务名，上游服务节点会通过服务名来查找</li> <li>protocol: 协议类型（目前支持 http 和 grpc）</li> <li>plugins: 路由启用的插件</li> <li>props: 路由参数配置，插件可以读取参数配置执行对应逻辑（例如: rewrite 插件）</li></ul></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/ws-cloud/gateway/1.x/plugin/" class="prev router-link-active">
        介绍
      </a></span> <span class="next"><a href="/ws-cloud/gateway/1.x/plugin/official/discovery.html">
        discovery plugin
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.92627f89.js" defer></script><script src="/assets/js/2.817fcd47.js" defer></script><script src="/assets/js/21.3ea5972a.js" defer></script>
  </body>
</html>
